/**
 * @author 陆文杰
 * @date 2025/9/26 11:04
 * @description 序列号控制器
 */
package com.ruoyi.web.controller.nongjt;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.nongjt.domain.SysPrintSequence;
import com.ruoyi.nongjt.service.ISysPrintSequenceService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/nongjt/printSequence")
public class SysPrintSequenceController extends BaseController {

    @Autowired
    private ISysPrintSequenceService printSequenceService;

    /** 列表查询（权限+分页） */
    @PreAuthorize("@ss.hasPermi('nongjt:printSequence:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysPrintSequence printSeq) {
        startPage();
        List<SysPrintSequence> list = printSequenceService.selectPrintSequenceList(printSeq);
        return getDataTable(list);
    }

    /** 按ID查询详情 */
    @PreAuthorize("@ss.hasPermi('nongjt:printSequence:query')")
    @GetMapping("/{seqId}")
    public AjaxResult getInfo(@PathVariable Long seqId) {
        return AjaxResult.success(printSequenceService.selectPrintSequenceById(seqId));
    }

    /** 新增 */
    @PreAuthorize("@ss.hasPermi('nongjt:printSequence:add')")
    @PostMapping
    public AjaxResult add(@Valid @RequestBody SysPrintSequence printSeq) {
        // printSeq.getCreateTime() 会自动解析为Date对象
        printSeq.setCreateBy(getUsername());
        return toAjax(printSequenceService.insertPrintSequence(printSeq));
    }

    /** 编辑 */
    @PreAuthorize("@ss.hasPermi('nongjt:printSequence:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody SysPrintSequence printSeq) {
        return toAjax(printSequenceService.updatePrintSequence(printSeq));
    }

    /**
     * 单行删除
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printSequence:remove')")
    @DeleteMapping("/{seqId}")
    public AjaxResult remove(@PathVariable Long seqId) {
        return toAjax(printSequenceService.deletePrintSequenceById(seqId));
    }

    /**
     * 导出打印序列号列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printSequence:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response, SysPrintSequence printSeq) {
        List<SysPrintSequence> list = printSequenceService.selectPrintSequenceList(printSeq);
        ExcelUtil<SysPrintSequence> util = new ExcelUtil<>(SysPrintSequence.class);
        util.exportExcel(response, list, "打印序列号数据");
    }


    /**
     * 导入打印序列号
     */
    @PreAuthorize("@ss.hasPermi('nongjt:printSequence:import')")
    @PostMapping("/import")
    public AjaxResult importData(@RequestParam("file") MultipartFile file
    ) throws Exception {
        ExcelUtil<SysPrintSequence> util = new ExcelUtil<>(SysPrintSequence.class);
        List<SysPrintSequence> printList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        int rows = printSequenceService.batchInsertPrintSequence(printList, operName);
        return toAjax(rows);
    }

    /**
     * 校验打印序列号唯一性
     */
    @GetMapping("/checkSerialUnique")
    public AjaxResult checkSerialUnique(String serialNumber) {
        return printSequenceService.checkSerialNumberUnique(serialNumber);
    }

}